{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79f6b261-523d-4928-bb97-a9dc3588e7c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# File: large-twist.ipynb\n",
    "# Code: Claude Code and Codex\n",
    "# Review: Ryoichi Ando (ryoichi.ando@zozo.com)\n",
    "# License: Apache v2.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24ef9f4b-2929-4e22-9d10-5ecff86ba720",
   "metadata": {},
   "outputs": [],
   "source": [
    "from frontend import App\n",
    "\n",
    "# create an app\n",
    "app = App.create(\"large-twist\")\n",
    "\n",
    "# create a large cylinder mesh\n",
    "V, F = app.mesh.cylinder(r=0.9, min_x=-1.75, max_x=1.75, n=1410)\n",
    "app.asset.add.tri(\"cylinder\", V, F)\n",
    "\n",
    "# create a scene\n",
    "scene = app.scene.create()\n",
    "\n",
    "# add cylinder with gradient coloring along axis\n",
    "obj = scene.add(\"cylinder\").cylinder_color([0, 0, 0], [1, 0, 0], [0, 1, 0])\n",
    "\n",
    "# set material properties\n",
    "(\n",
    "    obj.param.set(\"bend\", 500.0)\n",
    "    .set(\"contact-gap\", 1.75e-3)\n",
    "    .set(\"young-mod\", 1e4)\n",
    "    .set(\"poiss-rat\", 0.25)\n",
    "    .set(\"density\", 3.5)\n",
    ")\n",
    "\n",
    "# define animation parameters for twisting and scaling\n",
    "angular_vel, move_delta, t_end = 180.0, 0.5, 16.66\n",
    "scale = 1 - t_end * 0.015\n",
    "\n",
    "# pin and animate left end\n",
    "(\n",
    "    obj.pin(obj.grab([-1, 0, 0]))\n",
    "    .spin(axis=[1, 0, 0], angular_velocity=angular_vel)\n",
    "    .scale(scale, t_start=0.0, t_end=t_end)\n",
    "    .move_by([move_delta, 0, 0], t_start=0.0, t_end=t_end)\n",
    ")\n",
    "\n",
    "# pin and animate right end in opposite direction\n",
    "(\n",
    "    obj.pin(obj.grab([1, 0, 0]))\n",
    "    .spin(axis=[-1, 0, 0], angular_velocity=angular_vel)\n",
    "    .scale(scale, t_start=0.0, t_end=t_end)\n",
    "    .move_by([-move_delta, 0, 0], t_start=0.0, t_end=t_end)\n",
    ")\n",
    "\n",
    "# compile the scene and report stats\n",
    "scene = scene.build().report()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e204b342-2428-4689-b7cc-7a71f13b6b80",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a new session with the compiled scene\n",
    "session = app.session.create(scene)\n",
    "\n",
    "# set session parameters - enable auto-save, disable gravity, and increase sparse matrix capacity\n",
    "(\n",
    "    session.param.set(\"auto-save\", 10)\n",
    "    .set(\"dt\", 1 / 60)\n",
    "    .set(\"frames\", 2000)\n",
    "    .set(\"gravity\", 0.0)\n",
    "    .set(\"csrmat-max-nnz\", 160000000)\n",
    "    .set(\"keep-verts\", 100)\n",
    ")\n",
    "\n",
    "# build this session\n",
    "session = session.build()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c27d5da1-dc23-410d-a6b8-5b10434fe1cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# start the simulation (this example takes a long time)\n",
    "session.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5e88e14-84f0-44f0-9540-42ac293f9135",
   "metadata": {},
   "outputs": [],
   "source": [
    "# this example takes a long time...\n",
    "# in case you shutdown the server (or kernel) and still want to restart\n",
    "# from where you have (auto) saved, do this. Do not call cells above.\n",
    "\n",
    "from frontend import App  # noqa\n",
    "\n",
    "# recover the session from auto-saved state\n",
    "session = App.recover(\"large-twist\")\n",
    "\n",
    "# resume if not currently running\n",
    "if not App.busy():\n",
    "    session.resume()\n",
    "\n",
    "# preview the current state\n",
    "session.preview()\n",
    "\n",
    "# stream the logs\n",
    "session.stream()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
